package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

public class YorkbbsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://[a-z]*.yorkbbs.ca/[a-z]*/[0-9]*.html",
		"http://[a-z]*.yorkbbs.ca/[a-z]*/[0-9]{4}-[0-9]{1,2}/[0-9]{4,}.html",
		"http://forum.yorkbbs.ca/private/[0-9]*.aspx",
	};

	
	public boolean isDetailPage(String url) {
		if(url.indexOf("/topics/")!=-1)
			return false;
		
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div.article-content header h1").text();
		if(StringUtils.isBlank(title))
			title =doc.select("h1#artibodyTitle").text();
		if(StringUtils.isBlank(title))
			title =doc.select("h2#artibodyTitle").text();
		if(StringUtils.isBlank(title))
			title =doc.select("span.V_title").text();
		
		if(date==null)
			date =DateUtils.matchDate(doc.select("div.article-meta").text());
		if(date == null)
			date =DateUtils.matchDate(doc.select("div.contTime").text());
		if(date == null)
			date =DateUtils.matchDate(doc.select("div.timely").text());
		if(date == null)
			date =DateUtils.matchDate(StringUtils.substringBetween(htmltxt, "<font class=\"toutime\">", "</font>"));

		author = JSoupUtils.matchAuthor(doc, "来源网站：");
		if(StringUtils.isBlank(author))
			author = JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isBlank(author)){
			Elements authorList = doc.select("div.viewContL h3");
			if(authorList.size() > 0)
				author = authorList.get(0).text();
		}
		
		content=HtmlCleaner.getContentHtml(url,doc.select("div.article-main"));
		if(StringUtils.isBlank(content))
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		if(StringUtils.isBlank(content))
			content=HtmlCleaner.getContentHtml(url,doc.select("div.View14"));
		
		/*
		 * 有一些页面采用了单独的样式，并且需要移除。
		 */
		if(StringUtils.isBlank(title))
		{
			date =DateUtils.matchDate(doc.select("span.f14").text());
			doc.select("div.cont h1 span.f14").remove();
			title =doc.select("div.cont h1").text();
			doc.select("div.cont h1").remove();
			content = doc.select("div.cont").text();
		}
			
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
